package org.team4.web.ajax;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.team4.constants.StringConstant;
import org.team4.domain.Intercom;
import org.team4.service.IntercomService;
import org.team4.util.DateTimeHelper;
import org.team4.vo.IntercomVO;

import com.opensymphony.xwork2.Action;

@Controller
@Scope("prototype")
public class IntercomAjaxAction {

    @Autowired
    private IntercomService intercomService;

    private String intercomId;
    private String result;
    private Integer noread;
    private List<IntercomVO> intercomList;

    /**
     * 将未读更新为已读,并返回其状态html代码
     * 
     * @return
     * @throws Exception
     */
    public String updateIntercomToRead() throws Exception {
        result = StringConstant.INTERCOM_SELECT.get(intercomService.updateView(intercomId));
        return Action.SUCCESS;
    }
    
    /**
     * 统计未读信息
     * 
     * @return
     * @throws Exception
     */
    public String findNoReadCount() throws Exception {
    	noread = intercomService.countNoRead();
    	return Action.SUCCESS;
    }
    
    /**
     * 查找未读信息
     * 
     * @return
     * @throws Exception
     */
    public String findNoRead() throws Exception {
    	List<Intercom> list = intercomService.findNoRead();
    	intercomList = new ArrayList<IntercomVO>();
    	for (Intercom intercom : list) {
    		IntercomVO vo = new IntercomVO();
    		vo.setId(intercom.getId());
    		vo.setSender(intercom.getSender().getRealName());
    		vo.setTitle(intercom.getTitle());
    		vo.setSendTime(DateTimeHelper.formatDateTime("yyyy-MM-dd HH:mm:ss", intercom.getSendTime()));
    		intercomList.add(vo);
		}
    	return Action.SUCCESS;
    }

    public String getIntercomId() {
        return intercomId;
    }

    public void setIntercomId(String intercomId) {
        this.intercomId = intercomId;
    }

    public String getResult() {
        return result;
    }

	public Integer getNoread() {
		return noread;
	}

	public List<IntercomVO> getIntercomList() {
		return intercomList;
	}
}